<html>
<head><meta charset="utf-8"><title>Is `String::new()` slower than `&quot;&quot;.to_string`? · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html">Is `String::new()` slower than `&quot;&quot;.to_string`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241801014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241801014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241801014">(Jun 07 2021 at 16:12)</a>:</h4>
<p>I was just comparing some assembly out of curiousity and came across this: &lt;<a href="https://rust.godbolt.org/z/P53MMG4W6">https://rust.godbolt.org/z/P53MMG4W6</a>&gt;. It appears that <code>String::new()</code> does a bit more than <code>"".to_string()</code>. Why is that? Is this a known bug, or is it even a bug? I believe they should do the same thing.</p>



<a name="241801470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241801470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241801470">(Jun 07 2021 at 16:15)</a>:</h4>
<p>The problem is probably because it copies its value from the constant <a href="https://github.com/rust-lang/rust/blob/d620ae10709ca3669cda89db8b29afcd9accc188/library/alloc/src/raw_vec.rs#L64">https://github.com/rust-lang/rust/blob/d620ae10709ca3669cda89db8b29afcd9accc188/library/alloc/src/raw_vec.rs#L64</a> instead of immediately creating the appropriate value</p>



<a name="241801547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241801547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241801547">(Jun 07 2021 at 16:16)</a>:</h4>
<p>so... probably a problem in our const codegen logic that causes LLVM to produce worse code than if the value is built from scratch</p>



<a name="241801682"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241801682" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> ruster <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241801682">(Jun 07 2021 at 16:17)</a>:</h4>
<p>I see. Is this worth creating an issue for?</p>



<a name="241801762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241801762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241801762">(Jun 07 2021 at 16:18)</a>:</h4>
<p>any hard to find codegen bug seems worth creating an issue for</p>



<a name="241802257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241802257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241802257">(Jun 07 2021 at 16:22)</a>:</h4>
<p>I think it is also not too complex to build a codegen test for it. This way we'll notice if it changes by accident, or when we actually fix it</p>



<a name="241802733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241802733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Yuki Okushi <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241802733">(Jun 07 2021 at 16:25)</a>:</h4>
<p>fwiw the behavior has been changed since 1.52.0 according to the godbolt</p>



<a name="241811202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241811202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241811202">(Jun 07 2021 at 17:25)</a>:</h4>
<p>Hmm, that looks like const promotion, but that's not a place where that would happen.</p>



<a name="241811381"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241811381" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241811381">(Jun 07 2021 at 17:26)</a>:</h4>
<p>Oh, there's a const for <code>RawVec::NEW</code> in <a href="https://github.com/rust-lang/rust/blob/e4a60327063e82413eed50a10df3b7d19b77bda0/library/alloc/src/vec/mod.rs#L419-L421">https://github.com/rust-lang/rust/blob/e4a60327063e82413eed50a10df3b7d19b77bda0/library/alloc/src/vec/mod.rs#L419-L421</a> -- maybe that's why it's doing this.</p>



<a name="241811593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241811593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241811593">(Jun 07 2021 at 17:28)</a>:</h4>
<p>But that's been there since 2019 (<a href="https://github.com/rust-lang/rust/pull/64028">https://github.com/rust-lang/rust/pull/64028</a>) so I guess it's a difference somewhere more subtle.</p>



<a name="241947628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Is%20%60String%3A%3Anew%28%29%60%20slower%20than%20%60%22%22.to_string%60%3F/near/241947628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Is.20.60String.3A.3Anew().60.20slower.20than.20.60.22.22.2Eto_string.60.3F.html#241947628">(Jun 08 2021 at 16:32)</a>:</h4>
<p>for the people following at home, an issue was indeed created to track this: <a href="https://github.com/rust-lang/rust/issues/86106">#86106</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>